package com.xxwy.study.server.handler;

import com.xxwy.study.common.Operation;
import com.xxwy.study.common.OperationResult;
import com.xxwy.study.common.RequestMessage;
import com.xxwy.study.common.ResponseMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * SimpleChannelInboundHandler： 自动释放byteBuf.
 *
 * @author xxwy
 * @date 2021/11/23 0023
 */
public class OrderServerProcessHandler extends SimpleChannelInboundHandler<RequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RequestMessage msg) throws Exception {
        Operation messageBody = msg.getMessageBody();
        OperationResult operationResult = messageBody.execute();
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMessageHeader(msg.getMessageHeader());
        responseMessage.setMessageBody(operationResult);
        // 从当前handler触发
        ctx.writeAndFlush(responseMessage);
    }
}
